package mybatisdemo.mapper

import com.tsp.ssm.po.Orderdetail
import mybatisdemo.po.Orders
import mybatisdemo.po.OrdersCustom
import mybatisdemo.po.User
import org.apache.ibatis.io.Resources
import org.apache.ibatis.session.SqlSession
import org.apache.ibatis.session.SqlSessionFactory
import org.apache.ibatis.session.SqlSessionFactoryBuilder

/**
 * Created by 隼 on 2016/12/7.
 */
class OrdersMapperCustomTest extends GroovyTestCase {
    private SqlSessionFactory sqlSessionFactory;
    // 此方法是在执行testFindUserById之前执行
    void setUp() {
        super.setUp()
        // 创建sqlSessionFactory
        //mybatis 配置文件
        String resource = "config/mybatis/sqlMaptest.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //创建会话工厂,传入mybatis 配置文件
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


    }

    void testFindOrdersUser() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建对象
        OrdersMapperCustom ordersMapperCustom=sqlSession.getMapper(OrdersMapperCustom.class);

        List<OrdersCustom> list=ordersMapperCustom.findOrdersUser();
        Iterator<OrdersCustom> iterator=list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next().toString())
        }
        sqlSession.close();
    }

    void testfindOrdersUserResultMap(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建对象
        OrdersMapperCustom ordersMapperCustom=sqlSession.getMapper(OrdersMapperCustom.class);

        List<Orders> list=ordersMapperCustom.findOrdersUserResultMap();
        Iterator<Orders> iterator=list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
        sqlSession.close();
    }

    void  testfindOrdersAndOrderDetailResultMap(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建对象
        OrdersMapperCustom ordersMapperCustom=sqlSession.getMapper(OrdersMapperCustom.class);
        List<Orders> list=ordersMapperCustom.findOrdersAndOrderDetailResultMap();
        Iterator<Orders> iterator1=list.iterator();
        while(iterator1.hasNext()){
            Orders orders=iterator1.next();
            System.out.println(orders.toString());
            Iterator<Orderdetail> iterator=orders.getOrderdetails().iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next().toString());
            }

        }
        sqlSession.close();
    }

    void testfindUserAndItemsResultMap(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建对象
        OrdersMapperCustom ordersMapperCustom=sqlSession.getMapper(OrdersMapperCustom.class);
        List<User> userList=ordersMapperCustom.findUserAndItemsResultMap();
        userList;
        sqlSession.close();

    }

    void testfindOrdersUserLazyLoading(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建对象
        OrdersMapperCustom ordersMapperCustom=sqlSession.getMapper(OrdersMapperCustom.class);
        //查询订单信息，单表
        List<Orders> list=ordersMapperCustom.findOrdersUserLazyLoading();
        for(Orders orders:list){
            User user=orders.getUser();
            orders.print();

        }
    }


    //一级缓存测试

     void testCache1() throws Exception{
         SqlSession sqlSession = sqlSessionFactory.openSession();// 创建代理对象
         UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
         // 下边查询使用一个SqlSession
         // 第一次发起请求，查询id为1的用户
         com.tsp.ssm.po.User user1 = userMapper.findUserById(1);
         System.out.println(user1);

         // 如果sqlSession去执行commit操作（执行插入、更新、删除），清空SqlSession中的一级缓存，这样做的目的为了让缓存中存储的是最新的信息，避免脏读。

         // 更新user1的信息
          user1.setUsername("测试用户22");
          userMapper.updateUser(user1);
         // //执行commit操作去清空缓存
          sqlSession.commit();
         // 第二次发起请求，查询id为1的用户
         com.tsp.ssm.po.User  user2 = userMapper.findUserById(1);
         System.out.println(user2);
         sqlSession.close();
    }


    // 二级缓存测试

     void testCache2() throws Exception {
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        SqlSession sqlSession3 = sqlSessionFactory.openSession();
        // 创建代理对象
         UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
         // 第一次发起请求，查询id为1的用户
         com.tsp.ssm.po.User user1 = userMapper1.findUserById(1);
         System.out.println(user1);
         //这里执行关闭操作，将sqlsession中的数据写到二级缓存区域
        sqlSession1.close();
         //		//使用sqlSession3执行commit()操作
//		UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);
//		User user  = userMapper3.findUserById(1);
//		user.setUsername("张明明");
//		userMapper3.updateUser(user);
//		//执行提交，清空UserMapper下边的二级缓存
//		sqlSession3.commit();
//		sqlSession3.close();

         UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
         // 第一次发起请求，查询id为1的用户
         com.tsp.ssm.po.User user2 = userMapper2.findUserById(1);
         System.out.println(user2);
         //这里执行关闭操作，将sqlsession中的数据写到二级缓存区域
         sqlSession2.close();




    }
}
